app.controller('goodsDetailAddCtrl', ['$scope', '$state', 'goodsDetailAddService', 'toaster', function ($scope, $state, goodsDetailAddService, toaster) {
    $scope.goodsDetailAddService = goodsDetailAddService;
    $scope.brands;
    $scope.types;
    $scope.skus = new Array();
    $scope.skuContents = new Array();
    $scope.systemTypesArr=[];

    $scope.goodsDetailAddService.goods.type=0;

    //查询连锁信息
    $scope.queryShop=function(){
        goodsDetailAddService.queryShop($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.chain=result.data;
                    goodsDetailAddService.goods=initGoods;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.queryShop();

    //查询品牌信息
    $scope.querybrands = function () {
        goodsDetailAddService.querybrands($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.brands = result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.querybrands();

    //查询类型信息
    $scope.queryType = function () {
        goodsDetailAddService.queryType($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.types = result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.queryType();

    //查询系统分类
    $scope.querySystemType = function () {
        goodsDetailAddService.querySystemType($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.systemTypes = result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.querySystemType();

    $scope.changeSystemType=function(obj){
        $scope.goodsDetailAddService.goods.systemTypeId=[];
        for(var i=0;i<obj.length;i++){
            $scope.goodsDetailAddService.goods.systemTypeId.push(obj[i].id);

        }
    }


    //查询运费模板
    $scope.queryPostageTemplate = function () {
        goodsDetailAddService.queryPostageTemplate($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.postageTemplate = result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.queryPostageTemplate();

    $scope.changeType=function(type){
        $scope.goodsDetailAddService.goods=initGoods;
        goodsDetailAddService.goods.type=type;
    };

    $scope.changeSellType=function(type){
        if(type==0){
            goodsDetailAddService.goods.isOpenSell=0;
            goodsDetailAddService.goods.isOpenRebate=0;
            goodsDetailAddService.goods.profit=0;
        }else if(type==1){
            goodsDetailAddService.goods.isOpenSell=1;
            goodsDetailAddService.goods.isOpenRebate=0;
            goodsDetailAddService.goods.profit=$scope.chain.profit;
        }else if(type==2){
            goodsDetailAddService.goods.isOpenSell=0;
            goodsDetailAddService.goods.isOpenRebate=1;
            if($scope.chain.profit<26){
                goodsDetailAddService.goods.profit=26;
            }else{
                goodsDetailAddService.goods.profit=$scope.chain.profit;
            }
        } else if(type==3){
            goodsDetailAddService.goods.isOpenSell=1;
            goodsDetailAddService.goods.isOpenRebate=1;
            if($scope.chain.profit<26){
                goodsDetailAddService.goods.profit=26;
            }else{
                goodsDetailAddService.goods.profit=$scope.chain.profit;
            }
        }else if(type == 4){
            goodsDetailAddService.goods.isOpenSell=0;
            goodsDetailAddService.goods.isOpenRebate=0;
            goodsDetailAddService.goods.profit=0;
        }
    }


    //添加商品信息
    var initGoods={
        isOpenSku: 0,
        isFreeShipping: 0,
        isOpenMember: 0,
        isRecommend: 0,
        isHelpPeasant:0,
        isOpenRebate: 0,
        postageType: 0,
        systemTypeId:[],
        type: 0,
        isOpenSell:0,
        profit:0,
        postageWeight:0.000,
        tagIds:[],
        techniciansId:[]
    };

    $scope.addState=false;

    $scope.addGoods = function () {
        if($scope.addState){
            return;
        }

        $scope.addState=true;

        goodsDetailAddService.addGoods($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.chainGoodsId=result.data.id;
                    if($scope.goodsDetailAddService.goods.isOpenSku==1){
                        $scope.saveGoodsSku(result.data.id);
                    }else{
                        debugger;
                        $state.go('app.goodsDetailPictures', {chainGoodsId: result.data.id});
                        $scope.goodsDetailAddService.goods = initGoods;
                        $scope.addState=false;
                        toaster.pop('success', "提示", "操作成功");
                    }
                    break;
                default :
                    $scope.addState=false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.saveGoodsSku=function(chainGoodsId){
        goodsDetailAddService.saveGoodsSku($scope).then(function(result){
            switch (result.status) {
                case 200:
                    $state.go('app.goodsDetailPictures', {chainGoodsId: chainGoodsId});
                    $scope.goodsDetailAddService.goods = initGoods;
                    $scope.addState=false;
                    toaster.pop('success', "提示", "操作成功");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });

    }


    $scope.$on('skuContents', function(event, data) {
        $scope.skuContents=data;
    });


    $scope.openSku=function(){
        if($scope.goodsDetailAddService.goods.isOpenSku==1){
            $scope.goodsDetailAddService.goods.price=0.00;
            $scope.goodsDetailAddService.goods.stock=0;
        }else if($scope.goodsDetailAddService.goods.isOpenSku==0){
            $scope.skus=[];
        }
    }


}])
